package com.conversationboard.batch.jobs.fulltext;

import java.io.IOException;
import java.sql.SQLException;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import com.conversationboard.fulltext.index.IndexNewDocuments;
import com.conversationboard.fulltext.index.ReindexDocuments;
import com.conversationboard.fulltext.search.Search;

/**
 * 
 * Batch job to index new documents and reindex changed documents. It implements StatefulJob, not because it
 * holds state, but this is the way by which in Quartz, you tell it not to run jobs concurrently. So in the
 * situation where you start up the server and there's a large database to index, it won't run other jobs
 * at the same time. They will fire at the end of it, but they should be quick to execute.
 * 
 * @author Keith Watson
 *
 */

public class IndexDocuments implements StatefulJob {

	public void execute(JobExecutionContext context) throws JobExecutionException {

		try {

			IndexNewDocuments indexNewDocuments = new IndexNewDocuments();
			indexNewDocuments.indexNewDocuments();

			ReindexDocuments reindexDocuments = new ReindexDocuments();
			reindexDocuments.reindexDocuments();

			Search.reinitialiseSearchers();

		} catch (SQLException e) {
			throw new JobExecutionException(e);
		} catch (IOException e) {
			throw new JobExecutionException(e);
		}

	}

}
